package com.zk.atm.service.impl;

import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.bpms.common.Base64;
import com.bpms.common.CommonUtil;
import com.bpms.common.JdbcWorker;
import com.bpms.common.Param;
import com.bpms.common.VO;
import com.bpms.eai.PackTool;
import com.bpms.esb.IService;
import com.service.util.BillPhoto;

public class SaveOrg extends PackTool implements IService {

	public VO exec(VO arg0) {
		CommonUtil.debug("SaveAtmpDevice begin");
		try {
			String path = Param.getValue("UAI", "bill_photo_save_path"); // 广州农行账号查询转发
			if (path != null) {
				int photocount = Integer.parseInt(arg0.getAttr("photocount"));
				long INSTANCEID = getBizid();
				
				String bizid = new SimpleDateFormat("yyMMddHHmmss").format(new Date()) + String.format("%08d", INSTANCEID);
				
				path += new SimpleDateFormat("yyyyMMdd").format(new Date()) + "\\"; // 按照日期保存

				String filename = arg0.getAttr("TRANTYPE") + "-" + bizid +"-"+ arg0.getAttr("EXCHANGEBANKNO") 
						+  "-" + arg0.getAttr("BILLNO"); // 凭证号
				

				for (int i = 1; i <= photocount; i++) {
					byte[] photo = Base64.decode(arg0.getAttr("photo" + i));
					String photo1name = filename + "-" + i + ".jpg";
					if (i < 3)
						arg0.setAttr("IMAGE" + i, path + photo1name);
					BillPhoto.save(path, photo1name, photo);
				}

				
				VO voSave = Save(arg0,INSTANCEID);
				JdbcWorker db=null;
//				if(id!=null){
//				db.execute("select from atmp_org o where o.id="+id);
//				}
//				else{
//					db.insert(vo);
//				}
				if ("1".equals(voSave.getAttr("ret"))) { // 保存数据失败
					bizid = voSave.getAttr("bizid");
				} else {
					CommonUtil.debug("Savebillimage save 2 db failed.");
				}
				CommonUtil.debug("Savebillimage " + voSave);
				
				
				return packResult(bizid);
			} else {
				CommonUtil.debug("Savebillimage image save path is null");
				return packResult("");
			}
		} catch (Exception e) {
			CommonUtil.debug("Savebillimage  error", e);
			return packResult("");
		}
	}

	private VO packResult(String bizid) {
		VO vo = new VO();
		String message = "";
		// R001 处理结果代码 0000:成功
		// R002 结果描述
		// R003 业务流水号
		// 字段长度 u_char[8] 字段值的长度，定长，位数不足前补0，全部为0则字段值为空
		// 字段编码 u_char[4] 字段编码，定长
		// 字段值 U_char 字段值

		// message = "00000090"; // 本报文长度 u_char[8]
		message = "00000000"; // 流水号 U_char[8]
		message += "00000000"; // 交易报文总长度 U_char[8] I
		message += "1008";// 交易码 u_char[4]
		message += "0000";// 应答结果 u_char[4]
		try {
			if (bizid.length() > 0) {
				message += "00000004R0010000";
				//message += "00000006R002sucess";
				String sucess = "接收票据影像成功";
				message += String.format("%08d", sucess.getBytes("gb2312").length) + "R002" + sucess;
				message += "00000020R003" + bizid;
			} else {
				
					message += "00000004R0019999";
					// message += "00000006R002failed";
					String error = "接收票据影像失败";
					message += String.format("%08d", error.getBytes("gb2312").length) + "R002" + error;
					message += "00000004R003" + String.format("%020d", 0);
			}
			message = String.format("%08d", message.getBytes("gb2312").length) + message;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		vo.setAttr("msg", message);
		CommonUtil.debug("Savebillimage packResult done");
		return vo;
	}

	private long getBizid() throws Exception{
		JdbcWorker j = new JdbcWorker();
		long INSTANCEID = j.getSeqValue("cmdjb_bizid");
		return INSTANCEID;	
	}
	private VO Save(VO arg0,long INSTANCEID) {
		JdbcWorker j = null;
		VO vo = new VO();
		try {
			j = new JdbcWorker();
			DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
			vo.setAttr("INSTANCEID", Long.toString(INSTANCEID));
			String bizid = new SimpleDateFormat("yyMMddHHmmss").format(new Date()) + String.format("%08d", INSTANCEID);
			vo.setAttr("BIZID", bizid); // 流水号
			vo.setAttr("DEVICENO", arg0.getAttr("DEVICENO")); // 设备号
			vo.setAttr("NETNUM", arg0.getAttr("NETNUM")); // 网点号
			Date dt = df.parse(arg0.getAttr("BillDateTime"));

			vo.setAttr("CREATEDATE", new SimpleDateFormat("yyyy-MM-dd").format(dt)); // 创建日期
			vo.setAttr("CREATETIME", new SimpleDateFormat("HH:mm:ss").format(dt)); // 穿件时间
			vo.setAttr("TRANTYPE", arg0.getAttr("TRANTYPE")); // 业务类型 //
			vo.setAttr("ACCOUNTTYPE", arg0.getAttr("ACCOUNTTYPE")); // 01本行/02他行
			vo.setAttr("CARDID", arg0.getAttr("CARDID")); // 卡号
			vo.setAttr("PAYEEACCNO", arg0.getAttr("PAYEEACCNO")); // 付款人
			vo.setAttr("DRAWEEACCNO", arg0.getAttr("DRAWEEACCNO")); // 收款人
			vo.setAttr("EXCHANGEBANKNO", arg0.getAttr("EXCHANGEBANKNO")); // 交换行号
			vo.setAttr("ISSUEDATE", arg0.getAttr("ISSUEDATE")); // 出票日期
			vo.setAttr("AMOUNT", arg0.getAttr("AMOUNT")); // 金额
			vo.setAttr("BILLNO", arg0.getAttr("BILLNO")); // 凭证号
			vo.setAttr("IMAGE1", arg0.getAttr("IMAGE1")); // 图像文件1
			vo.setAttr("IMAGE2", arg0.getAttr("IMAGE2")); // 图像文件2
			vo.setAttr("PASSWORD", arg0.getAttr("PASSWORD")); // 密码
			vo.setAttr("STATUS", "0"); // 密码
			vo.voType = "CM_DJB";
			j.insert(vo);
			vo.setAttr("ret", "1");
			vo.setAttr("msg", "SUCESS");
		} catch (Exception e) {
			CommonUtil.debug("Getcontract exception", e);
			vo.setAttr("ret", "0");
			vo.setAttr("msg", e.toString());
		}
		return vo;
	}
}
